package tbs.framework.async.task.container

import cn.hutool.extra.spring.SpringUtil
import tbs.framework.async.container.IAsyncTaskContainer
import tbs.framework.async.container.model.AsyncTaskDetail
import tbs.framework.async.task.IAsyncTask
import java.util.stream.Collectors

class SpringAsyncContainer : IAsyncTaskContainer {
    private val tasksMap: MutableMap<String, AsyncTaskDetail> = HashMap<String, AsyncTaskDetail>();

    public constructor(taskList: List<IAsyncTask>) {
        taskList.forEach {
            val r = AsyncTaskDetail(it);
            tasksMap.putIfAbsent(r.taskId, r);
        }
    }

    public constructor() : this(
        SpringUtil.getBeansOfType(IAsyncTask::class.java).values.stream().collect(Collectors.toList())
    ) {

    }

    @Deprecated("task auto manage by spring")
    override fun addTask(task: IAsyncTask): IAsyncTaskContainer {
        TODO("task auto manage by spring")
    }

    @Deprecated("task auto manage by spring")
    override fun removeTask(taskId: String): IAsyncTaskContainer {
        TODO("task auto manage by spring")
    }

    @Deprecated("task auto manage by spring")
    override fun clearTask(): IAsyncTaskContainer {
        TODO("task auto manage by spring")
    }

    override fun getTaskCount(): Int {
        return tasksMap.size
    }

    override fun getTaskDetail(taskId: String): AsyncTaskDetail? {
        return tasksMap[taskId];
    }

    override fun tasks(): MutableList<AsyncTaskDetail> {
        return ArrayList(tasksMap.values)
    }
}